using CodePlex.TfsLibrary.ObjectModel;
using CodePlex.UnitTestUtility;
using Xunit;

namespace CodePlex.CodePlexClientLibrary
{
    public class ApplyPatchPresenterTest
    {
        [Fact]
        public void CallsApplyPatch()
        {
            TestableApplyPatchPresenter presenter = TestableApplyPatchPresenter.Create();
            presenter.FileSystem.WriteAllText(@"C:\patch.xml", "This is my XML");

            presenter.ApplyPatch(@"C:\Foo", @"C:\patch.xml");

            Assert.Equal(@"C:\Foo", presenter.PatchEngine.ApplyPatch_Directory);
            Assert.Equal(@"This is my XML", presenter.PatchEngine.ApplyPatch_PatchXml);
        }

        [Fact]
        public void CallsViewWithStatus()
        {
            SourceItem item = new SourceItem();
            TestableApplyPatchPresenter presenter = TestableApplyPatchPresenter.Create();
            presenter.PatchEngine.ApplyPatch__CallbackData.Add(new SourceItemCallbackData(item, SourceItemResult.S_Ok));
            presenter.FileSystem.WriteAllText(@"C:\patch.xml", "This is my XML");

            presenter.ApplyPatch(@"C:\Foo", @"C:\patch.xml");

            Assert.Equal(item, presenter.View.ShowStatus_Item);
            Assert.Equal(SourceItemResult.S_Ok, presenter.View.ShowStatus_Result);
        }

        class TestableApplyPatchPresenter : ApplyPatchPresenter
        {
            public MockFileSystem FileSystem;
            public StubPatchEngine PatchEngine;
            public StubApplyPatchView View;

            TestableApplyPatchPresenter(StubPatchEngine patchEngine,
                                        MockFileSystem fileSystem,
                                        StubApplyPatchView view)
                : base(patchEngine, fileSystem, view)
            {
                PatchEngine = patchEngine;
                FileSystem = fileSystem;
                View = view;
            }

            public static TestableApplyPatchPresenter Create()
            {
                return new TestableApplyPatchPresenter(new StubPatchEngine(),
                                                       new MockFileSystem(),
                                                       new StubApplyPatchView());
            }
        }
    }
}